﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Dune.Business.Domain.AssemblyCodes;
using Edge.Business.Domain;
using Edge.Common.ComponentModel;
using Edge.Common.Services;
using Edge.DAL;
using Edge.DAL.Common.Config;
using Edge.DAL.Common.SessionManagment;
using Edge.DAL.Managers;
using NHibernate;
using NHibernate.Cfg;
using Shop.Business.Domain.Catalog;
using Shop.Business.Domain.Catalog.Parameter;
using Shop.Business.Domain.Customers;
using Shop.Business.Domain.Orders;
using Shop.Business.Domain.Pricing;
using Shop.DAL.Managers;

namespace Shop.DAL
{
    public class ShopInitializer : Initializer
    {
        public static void Setup(bool useCache)
        {

            Configuration cfg = new Configuration();

            cfg.Properties["hibernate.connection.provider"] = "NHibernate.Connection.DriverConnectionProvider";
            cfg.Properties["hibernate.connection.isolation"] = "ReadCommitted";
            cfg.Properties["hibernate.dialect"] = "NHibernate.Dialect.MsSql2005Dialect";
            cfg.Properties["hibernate.connection.driver_class"] = "NHibernate.Driver.SqlClientDriver";
            cfg.Properties["hibernate.use_outer_join"] = "true";

            if (useCache)
            {
                cfg.Properties["hibernate.cache.use_second_level_cache"] = "true";
                cfg.Properties["hibernate.cache.use_query_cache"] = "true";
                cfg.Properties["hibernate.cache.provider_class"] = "NHibernate.Caches.SysCache.SysCacheProvider, NHibernate.Caches.SysCache";
                //            cfg.Properties["hibernate.cache.provider_class"] = "NHibernate.Caches.Prevalence.PrevalenceCacheProvider, NHibernate.Caches.Prevalence";
                //                cfg.Properties["hibernate.cache.provider_class"] = "NHibernate.Cache.HashtableCacheProvider, NHibernate";                
            }


            DALSection dalSection = (DALSection)System.Configuration.ConfigurationManager.GetSection("dal");
            if (dalSection == null)
                throw new ApplicationException("dal section missed");
            cfg.Properties["hibernate.connection.connection_string"] =
                dalSection.ConnectionString;


            cfg.AddAssembly("Shop.DAL");




            ISessionFactory factory = cfg.BuildSessionFactory();

            Locator.AddService<ISessionFactory>(factory);

            Locator.AddService<EntityCodeManager>(new EntityCodeManager());

            // setup managers
            // TODO: find managers by Reflection API

            EntityLoader loader = new EntityLoader();
            Locator.AddService<IEntityLoader>(loader);
            Locator.AddService<IEntitySaver>(loader);



            // interceptors
            Locator.AddService<IInterceptorProvider>(new SessionInterceptor());


            // AssemblyStorage
//            AssemblyStorage assemblyStorage = new AssemblyStorage();
//            Locator.AddService<IAssemblyRepository>(assemblyStorage);
//            Locator.AddService<IAssemblyCodeManager>(assemblyStorage);
            
            Locator.AddService<ICustomerManager>(new CustomerManager());
            Locator.AddService<ICategoryManager>(new CategoryManager());
            Locator.AddService<IBrandManager>(new BrandManager());
            Locator.AddService<IProductGroupManager>(new ProductGroupManager());
            Locator.AddService<IProductManager>(new ProductManager());
            Locator.AddService<ICustomerAddressManager>(new CustomerAddressManager());
            Locator.AddService<IDeliveryTypeManager>(new DeliveryTypeManager());
            Locator.AddService<IOrderManager>(new OrderManager());
            Locator.AddService<IOrderItemManager>(new OrderItemManager());
            Locator.AddService<IParameterManager>(new ParameterManager());
            Locator.AddService<IParameterValueManager>(new ParameterValueManager());
            Locator.AddService<IPriceItemManager>(new PriceItemManager());
            Locator.AddService<IMenuItemManager>(new MenuItemManager());
            Locator.AddService<ISizeValueManager>(new SizeValueManager());


        }

    }
}
